﻿using System.Linq.Expressions;
using System.Text;

using Tszy.Unlimited.Data.AdoNet.Sql.Descriptors;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components.Expressions.Where.Logical
{
    /// <summary>
    ///
    /// </summary>
    internal abstract class LogicalWhereExpressionHandlerBase : IWhereExpressionHandler
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public WhereExpressionResult Execute(ExpressionContext context, Expression expression)
        {
            //a && b
            var binaryExpression = expression as BinaryExpression;

            var result = new WhereLogicalExpressionDescriptor();

            var left = new WhereLogicalExpressionParameter();

            var leftResult = WhereExpressionHandler.Instance.Execute(context, binaryExpression?.Left);

            if (leftResult.LogicalExpressionDescriptor != null)
            {
                left.LogicalExpressionDescriptor = leftResult.LogicalExpressionDescriptor;
            }

            if (leftResult.ComparisonExpressionDescriptor != null)
            {
                left.ComparisonExpressionDescriptor = leftResult.ComparisonExpressionDescriptor;
            }

            if (leftResult.MemberExpressionDescriptor != null)
            {
                left.MemberExpressionDescriptor = leftResult.MemberExpressionDescriptor;
            }

            if (leftResult.CallExpressionDescriptor != null)
            {
                left.CallExpressionDescriptor = leftResult.CallExpressionDescriptor;
            }

            var right = new WhereLogicalExpressionParameter();

            var rightResult = WhereExpressionHandler.Instance.Execute(context, binaryExpression?.Right);

            if (rightResult.LogicalExpressionDescriptor != null)
            {
                right.LogicalExpressionDescriptor = rightResult.LogicalExpressionDescriptor;
            }

            if (rightResult.ComparisonExpressionDescriptor != null)
            {
                right.ComparisonExpressionDescriptor = rightResult.ComparisonExpressionDescriptor;
            }

            if (rightResult.MemberExpressionDescriptor != null)
            {
                right.MemberExpressionDescriptor = rightResult.MemberExpressionDescriptor;
            }

            if (rightResult.CallExpressionDescriptor != null)
            {
                right.CallExpressionDescriptor = rightResult.CallExpressionDescriptor;
            }

            result.LogicalTag = LogicalTag();
            result.Left = left;
            result.Right = right;

            return new WhereExpressionResult { Status = true, LogicalExpressionDescriptor = result };
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public abstract ExpressionType GetExpressionType();

        /// <summary>
        /// “与”或者 || 符号。
        /// </summary>
        /// <returns></returns>
        protected abstract string LogicalTag();
    }
}